<template>
  <van-nav-bar :title="t('home.打印机设置')" left-arrow @click-left="onClickLeft" />
  <div class="p-2 mx-auto">
    <Picker :state="statePrint" :title="t('labelPrint.请选择打印机')" @close="closePopupPrint" @confirm="handleChangePrint" @reload="reloadPrint"/> 
    <van-cell
      :title="t('labelPrint.打印机')"
      :value="statePrint.currentItem ? statePrint.currentItem.text : t(`labelPrint.请选择打印机`)"
      icon="orders-o"
      url=""
      to="" 
      clickable
      is-link
      center
      @click="showPopupPrint"
    />
  </div>
</template>

<script setup lang="ts"> 
import { localStorage } from "@/utils/local-storage"; 
// import {
//   postRequest, 
// } from "@/api";
import { useI18n } from "vue-i18n"; 
import { 
  STORAGE_PRINTER_KEY,
} from "@/stores/mutation-type";
import { AppToast } from "@/utils/appToast"; 
import { getPrinter } from "@/utils/print";
const { t } = useI18n();

const statePrint = reactive({
  show: false,
  option: [],
  currentItem: null,
}); 
const reloadPrint = () => {
  // postRequest("/mock",[
  //   {
  //     value: "123456",
  //     text: "dyj01",
  //   },
  //   {
  //     value: "dyj02",
  //     text: "dyj02",
  //   },
  // ]).then((res) => { 
  //   if (res.status == 200) {
  //     statePrint.option = res.data;
  //     if (localStorage.get(STORAGE_PRINTER_KEY)) {
  //   statePrint.currentItem = statePrint.option.find(
  //     (item) => item.value === localStorage.get(STORAGE_PRINTER_KEY)
  //   ); 
  // }
  //   } else {
  //     AppToast(t('labelPrint.获取打印机失败')+" "+res.message,"danger");
  //   }
  // }); 

  // "https://print.j-net.cn/status/printers"
  // const request = axios.create({
  //   // API 请求的默认前缀
  //   baseURL: process.env.VUE_APP_API_BASE_URL_PRINT,
  //   method: "get",
  //   timeout: 6000, 
  //   headers: {
  //     // "authorization": "Basic cHJpbnQ6ZTMwNWM5ZjA3ZTlhMDg2MzViYzM5NmEzMzE4MzFjYmM=",
  //     "authorization": "Basic " + btoa("print:e305c9f07e9a08635bc396a331831cbc"),

  //   },
  // })
  // request.get("/status/printers").then((res) => { 
  //   if (res.status == 200) {
  //     statePrint.option = res.data;
  //     if (localStorage.get(STORAGE_PRINTER_KEY)) {
  //   statePrint.currentItem = statePrint.option.find(
  //     (item) => item.value === localStorage.get(STORAGE_PRINTER_KEY)
  //   ); 
  // }
  //   } else {
  //     AppToast(t('labelPrint.获取打印机失败')+" "+res.message,"danger");
  //   }
  // });

  getPrinter( 
            (res) => {
              for (let i = 0; i < res.data.length; i++) {
                statePrint.option.push({
                  value: res.data[i],
                  text: res.data[i],
                });
                console.log({
                  value: res.data[i],
                  text: res.data[i],
                })
              }
              console.log(statePrint)

              if (localStorage.get(STORAGE_PRINTER_KEY)) {
                statePrint.currentItem = statePrint.option.find(
                (item) => item.value === localStorage.get(STORAGE_PRINTER_KEY)
              ); 
            }
            },
            (res) => { 
              AppToast(t('labelPrint.获取打印机失败')+" "+res.message,"danger");
            }
          );

};
reloadPrint();



// 打印机
const showPopupPrint = () => {
  statePrint.show = true;
};
const handleChangePrint = (e: any): void => {
  const value = e.selectedOptions[0].value; 
  console.log(value)
  localStorage.set(STORAGE_PRINTER_KEY, value);
  // localStorage.setItem(STORAGE_PRINTER_KEY, value);
  statePrint.currentItem = statePrint.option.find((item) => item.value === value);
  statePrint.show = false;
  AppToast(t("labelPrint.切换成功"));
}; 
const closePopupPrint = () => { 
  statePrint.show = false;
}; 
const onClickLeft = () => history.back(); 


</script>

<style lang="less" scoped></style>
